package com.yanfan.zutai.controller;

import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.domain.model.LoginUser;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.zutai.aop.SysInitData;
import com.yanfan.zutai.aop.SysInitUpdateData;
import com.yanfan.zutai.aop.SysQueryData;
import com.yanfan.zutai.domain.BDeviceWarn;
import com.yanfan.zutai.domain.BDeviceZtBind;
import com.yanfan.zutai.domain.BWorkOrder;
import com.yanfan.zutai.domain.vo.BDeviceWorkVo;
import com.yanfan.zutai.service.BCommonService;
import com.yanfan.zutai.service.IBDeviceManagerService;
import com.yanfan.zutai.service.IBDeviceWarnService;
import com.yanfan.zutai.service.IBWorkOrderService;
import com.yanfan.zutai.util.Detect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 工单管理Controller
 *
 * @author ruoyi
 * @date 2021-02-27
 */
@RestController
@RequestMapping("/zutai/bWorkOrder")
public class BWorkOrderController extends BaseController {
    @Autowired
    private IBWorkOrderService bWorkOrderService;
    @Autowired
    private BCommonService bCommonService;
    @Autowired
    private IBDeviceWarnService bDeviceWarnService;
    @Autowired
    private IBDeviceManagerService bDeviceManagerService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询工单管理列表
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bWorkOrder:list')")
    @GetMapping("/list")
    @SysQueryData
    public TableDataInfo list(BWorkOrder bWorkOrder) {
        startPage();
        List<String> imeiList = new ArrayList<>();
        if (Detect.notEmpty(bWorkOrder.getZtGuid())) {
            bWorkOrder.setDeviceMac("");
            String ztSql = "select * from b_device_zt_bind where zt_guid=?";
            BeanPropertyRowMapper<BDeviceZtBind> bindBeanPropertyRowMapper = new BeanPropertyRowMapper<>(BDeviceZtBind.class);
            List<BDeviceZtBind> ztBinds = jdbcTemplate.query(ztSql, bindBeanPropertyRowMapper, bWorkOrder.getZtGuid());
            for (int i = 0; i < ztBinds.size(); i++) {
                imeiList.add(ztBinds.get(i).getDeviceMac());
            }
            bWorkOrder.setImeiList(imeiList);
        }
        List<BWorkOrder> list = bWorkOrderService.selectBWorkOrderList(bWorkOrder);
        return getDataTable(list);
    }

    /**
     * 导出工单管理列表
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bWorkOrder:export')")
    @Log(title = "工单管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @SysQueryData
    public AjaxResult export(BWorkOrder bWorkOrder) {
        List<BWorkOrder> list = bWorkOrderService.selectBWorkOrderList(bWorkOrder);
        ExcelUtil<BWorkOrder> util = new ExcelUtil<BWorkOrder>(BWorkOrder.class);
        return util.exportExcel(list, bWorkOrder.getType());
    }

    /**
     * 获取工单管理详细信息
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bWorkOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bWorkOrderService.selectBWorkOrderById(id));
    }

    /**
     * 新增工单管理
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bWorkOrder:add')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping
    @SysInitData
    public AjaxResult add(@RequestBody BWorkOrder bWorkOrder) {
        if (bWorkOrder.getWarnId() != null) {
            BDeviceWarn bDeviceWarn = bDeviceWarnService.selectBDeviceWarnById(bWorkOrder.getWarnId());
            bDeviceWarn.setProcessState(1);
            bDeviceWarnService.updateBDeviceWarn(bDeviceWarn);
        }
        return toAjax(bWorkOrderService.insertBWorkOrder(bWorkOrder));
    }

    /**
     * 修改工单管理
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bWorkOrder:edit')")
    @Log(title = "工单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @SysInitUpdateData
    public AjaxResult edit(@RequestBody BWorkOrder bWorkOrder) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (bWorkOrder.getProcessState() != null) {
            if (bWorkOrder.getProcessState() == 2 || bWorkOrder.getProcessState() == 3) {
                bWorkOrder.setShutPeople(loginUser.getUser().getNickName());
                bWorkOrder.setShutTime(new Date());
            }
        }
        return toAjax(bWorkOrderService.updateBWorkOrder(bWorkOrder));
    }

    /**
     * 删除工单管理
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bWorkOrder:remove')")
    @Log(title = "工单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bWorkOrderService.deleteBWorkOrderByIds(ids));
    }

    /**
     * 获取工单编号
     */
    @GetMapping(value = "/getWorkNumber")
    public AjaxResult getWorkNumber(BDeviceWorkVo bDeviceWorkVo) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        bDeviceWorkVo.setDeptId(loginUser.getUser().getDeptId() + "");
        return AjaxResult.success(bCommonService.getWorkNumber(bDeviceWorkVo.getWorkOrderType(), bDeviceWorkVo.getDeptId()));
    }

}
